﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CodeArt.SharePoint.Configuration
{
    using System.Configuration;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;

    /// <summary>
    /// ConfigurationElementBase
    /// </summary>
    public class ConfigurationElementBase : ConfigurationElement
	{
        /// <summary>
        /// Name
        /// </summary>
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
        }
	}

    public class NameValueElement : ConfigurationElementBase
    {
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("value", IsRequired = false)]
        public virtual string Value
        {
            get { return (string)this["value"]; }
        }

        public NameValueElement() { }
    }

    public class NameValueCollection : ConfigurationElementCollection
    {

        public NameValueElement this[object key]
        {
            get
            {
                return base.BaseGet(key) as NameValueElement;
            }
        }

        public NameValueElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as NameValueElement;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                //
                this.BaseAdd(index, value);
            }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        public NameValueCollection() { }

        protected override ConfigurationElement CreateNewElement()
        {
            return new NameValueElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((NameValueElement)element).Name;
        }

    }


}
